package com.suda.controller;

import com.github.pagehelper.PageInfo;
import com.suda.pojo.*;
import com.suda.service.*;
import entity.DeliverExample;
import entity.Result;
import entity.StatusCode;
import entity.WareHouseExample;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/deliver")
@CrossOrigin
public class DeliverConroller {
    @Autowired
    private RouteService routeService;

    @Autowired
    private WarehouseService warehouseService;

    @Autowired
    private StaffService staffService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private  OrderRouteService orderRouteService;

    @Autowired
    private StoreService storeService;

    //查询order_id='1'：离开某仓库，发往下一仓库
    @GetMapping(value = "/warehouse/{id}")
    Result<List<WareHouseExample>> warehouse(@PathVariable String id)
    {
        OrderRoute orderRoute= new OrderRoute();
        orderRoute.setOrderId(id);
        List<OrderRoute> routeids=orderRouteService.findList(orderRoute);
        List<WareHouseExample> wareHouseExamples=new ArrayList<>();
        for (OrderRoute routeid:routeids
        ) {
            Route route= routeService.findById(routeid.getRouteId());
            Warehouse warehouse=warehouseService.findById(route.getWarehouseId());
            Warehouse towarehouse=warehouseService.findById(route.getToWarehouseId());
            WareHouseExample res=new WareHouseExample(route.getRouteId(),route.getWarehouseId(),warehouse.getWarehouseName(),route.getToWarehouseId(),towarehouse.getWarehouseName(),route.getArrivalTime(),route.getDepartureTime());
            wareHouseExamples.add(res);
        }
        Collections.sort(wareHouseExamples, Comparator.comparing(WareHouseExample::getArrivalTime));
        return new Result(true, StatusCode.OK,"查询成功",wareHouseExamples);
    }

    //查询order_id='1'：揽件时间
    @GetMapping(value = "/time/{id}")
    Result<Date> getTime(@PathVariable String id)
    {
        Order order = orderService.findById(id);
        Date date=order.getReceiveTime();
        return new Result(true, StatusCode.OK,"查询成功",date);
    }

    //查询order_id='1'：揽件员，揽件点
    @GetMapping(value = "/name/{id}")
    Result<DeliverExample> getName(@PathVariable String id)
    {
        Order order=orderService.findById(id);
        Staff staff=staffService.findById(order.getReceiveCourierId());
        Warehouse warehouse=warehouseService.findById(staff.getWarehouseId());
        DeliverExample res =new DeliverExample(staff.getStaffId(),staff.getStaffName(),warehouse.getWarehouseId(),warehouse.getWarehouseName(),staff.getStaffPhone());
        return new Result(true, StatusCode.OK,"查询成功",res);
    }

    //查询order_id='1'：派送员名字，派送员电话，所在网点名字，所在网点电话
    //（t_order可增加快递派送状态）
    @GetMapping(value = "/send/{id}")
    public Result<DeliverExample> Send(@PathVariable String id)
    {
        Order order=orderService.findById(id);
        Staff staff=staffService.findById(order.getDistributerId());
        Warehouse warehouse=warehouseService.findById(staff.getWarehouseId());
       DeliverExample res=new DeliverExample(order.getDistributerId(),staff.getStaffName(),staff.getStaffPhone(),warehouse.getWarehouseId(),warehouse.getWarehouseName(),warehouse.getWarehousePhone());
        return new Result(true, StatusCode.OK,"查询成功",res);
    }

    //查询order_id，查询入库记录
    @GetMapping(value = "/In/{id}")
    public Result<List<DeliverExample>> In(@PathVariable String id)
    {
        Store store=new Store();
        store.setOrderId(id);
        List<Store> storeList=storeService.findList(store);
        List<WareHouseExample> wareHouseExamples=new ArrayList<>();
        for (Store storeid:storeList
        ) {Warehouse warehouse=warehouseService.findById(storeid.getWarehouseId());
            WareHouseExample wareHouseExample=new WareHouseExample(warehouse.getWarehouseName(),storeid.getStoreTime(),null);
            wareHouseExamples.add(wareHouseExample);
        }
        Collections.sort(wareHouseExamples, Comparator.comparing(WareHouseExample::getStoreTime));
        return new Result(true, StatusCode.OK,"查询成功",wareHouseExamples);
    }

    //查询order_id，查询出库记录
    @GetMapping(value = "/Out/{id}")
    public Result<List<WareHouseExample>> Out(@PathVariable String id)
    {
        Store store=new Store();
        store.setOrderId(id);
        List<Store> storeList=storeService.findList(store);
        List<WareHouseExample> wareHouseExamples=new ArrayList<>();
        for (Store storeid:storeList
             ) {Warehouse warehouse=warehouseService.findById(storeid.getWarehouseId());
            WareHouseExample wareHouseExample=new WareHouseExample(warehouse.getWarehouseName(),null,storeid.getDeliverTime());
             wareHouseExamples.add(wareHouseExample);
        }
        Collections.sort(wareHouseExamples, Comparator.comparing(WareHouseExample::getDeliverTime));
        return new Result(true, StatusCode.OK,"查询成功",storeList);
    }
}
